package com.pa.scratch.controller;


import com.github.pagehelper.PageInfo;
import com.lww.springcloudapi.entity.TabGgcardEncash;
import com.lww.springcloudapi.entity.TabGgcardObtain;
import com.lww.springcloudapi.result.Result;
import com.lww.springcloudapi.result.ResultCodeEnum;
import com.pa.scratch.mapper.TabGgcardEncashMapper;
import com.pa.scratch.service.TabGgcardEncashService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModel;
import lombok.val;
import org.apache.poi.hssf.usermodel.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 刮刮卡兑奖日志表 前端控制器
 * </p>
 *
 * @author SHEN
 * @since 2021-03-11
 */
@Api(description = "兑奖情况日/月报表")
@RestController
@RequestMapping("/userTest/tab-ggcard-encash")
public class TabGgcardEncashController {
    @Autowired
    TabGgcardEncashService tabGgcardEncashService;
    @PostMapping("/scratch/selectEncashAll")
    public Result selectList(@RequestParam(defaultValue = "1") int pageNo,
                             @RequestParam(defaultValue = "2") int pageSize,
                             @RequestParam(required = false, defaultValue = "") String acptsiteid,
                             @RequestParam(required = false, defaultValue = "") String svcid,
                             @RequestParam(required = false, defaultValue = "") String gifttype,
                             @RequestParam(required = false, defaultValue = "") String obtainoptrdate,
                             @RequestParam(required = false, defaultValue = "") String encashoptrdate) {
        Map map = new HashMap();
        map.put("acptsiteid",acptsiteid);
        map.put("svcid",svcid);
        map.put("gifttype",gifttype);
        map.put("obtainoptrdate",obtainoptrdate);
        map.put("encashoptrdate",encashoptrdate);
        try {
            List<TabGgcardEncash> tabGgcardEncashes = tabGgcardEncashService.selectEncashAll(map, pageNo, pageSize);
//            for (TabGgcardEncash tabGgcardEncashes1 :
//                    tabGgcardEncashes) {
//                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
//                String time = format.format(tabGgcardEncashes1.getOptrdate());//这个就是把时间戳经过处理得到期望格式的时间
//                String time2 = format.format(tabGgcardEncashes1.getTabGgcardObtain().getOptrdate());//这个就是把时间戳经过处理得到期望格式的时间
//                System.out.println(time+"========="+time2);
//                System.out.println(tabGgcardEncashes1.getTabGgcardObtain().getOptrdate()+"========="+tabGgcardEncashes1.getOptrdate());
//                System.out.println(tabGgcardEncashes1.toString());
//            }


            PageInfo<TabGgcardEncash> pageInfo = new PageInfo<TabGgcardEncash>(tabGgcardEncashes);
            return new Result().ok(pageInfo);
        } catch (Exception e) {
            return new Result().fail(ResultCodeEnum.FAIL);
        }
    }

    @GetMapping(value = "/scratch/ReportOfPrizeExcelDownloads")
    public Result downloadAllClassmate(HttpServletResponse response,
                                       @RequestParam(required = false, defaultValue = "1") int pageNo,
                                       @RequestParam(required = false, defaultValue = "2") int pageSize,
                                       @RequestParam(required = false, defaultValue = "") String acptsiteid,
                                       @RequestParam(required = false, defaultValue = "") String svcid,
                                       @RequestParam(required = false, defaultValue = "") String gifttype,
                                       @RequestParam(required = false, defaultValue = "") String obtainoptrdate,
                                       @RequestParam(required = false, defaultValue = "") String encashoptrdate) throws IOException {
        HSSFWorkbook workbook = new HSSFWorkbook();
        HSSFSheet sheet = workbook.createSheet("信息表");

        Map map = new HashMap();
        map.put("acptsiteid",acptsiteid);
        map.put("svcid",svcid);
        map.put("gifttype",gifttype);
        map.put("obtainoptrdate",obtainoptrdate);
        map.put("encashoptrdate",encashoptrdate);

        List<TabGgcardEncash> tabGgcardEncashes = tabGgcardEncashService.selectEncashAll(map, pageNo, pageSize);
//        for (TabGgcardEncash tabGgcardEncashes1 :
//                tabGgcardEncashes) {
//            System.out.println(tabGgcardEncashes1.toString());
//        }
        String fileName = "兑奖情况日/月报表" + ".xls";//设置要导出的文件的名字
        //新增数据行，并且设置单元格数据

        int rowNum = 1;

        String[] headers = {"刮奖卡序号", "营业厅", "业务类型", "奖品等级", "奖品类型", "参与活动日期", "领取日期", "用户手机号"};
        //headers表示excel表中第一行的表头
        HSSFRow row = sheet.createRow(0);
        //在excel表中添加表头

        for (int i = 0; i < headers.length; i++) {
            HSSFCell cell = row.createCell(i);
            HSSFRichTextString text = new HSSFRichTextString(headers[i]);
            cell.setCellValue(text);
        }
        //在表中存放查询到的数据放入对应的列
        for (TabGgcardEncash tabGgcardEncashes1 : tabGgcardEncashes) {
            HSSFRow row1 = sheet.createRow(rowNum);
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            String ObtainDate = format.format(tabGgcardEncashes1.getTabGgcardObtain().getOptrdate());//这个就是把时间戳经过处理得到期望格式的时间
            String EncashDate = format.format(tabGgcardEncashes1.getOptrdate());
            row1.createCell(0).setCellValue(tabGgcardEncashes1.getCardno());
            row1.createCell(1).setCellValue(tabGgcardEncashes1.getTabCode().getCodeName());
            row1.createCell(2).setCellValue(tabGgcardEncashes1.getTabGgcardObtain().getSvcid());
            row1.createCell(3).setCellValue(tabGgcardEncashes1.getPrizelevel());
            row1.createCell(4).setCellValue(tabGgcardEncashes1.getTabSaleplanGiftcode().getGiftname());
            row1.createCell(5).setCellValue(ObtainDate);
            row1.createCell(6).setCellValue(EncashDate);
            row1.createCell(7).setCellValue(tabGgcardEncashes1.getTabHallUsermessage().getPnumber());
            rowNum++;
        }
        response.setContentType("application/octet-stream");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName);
        response.flushBuffer();

        workbook.write(response.getOutputStream());

        return Result.ok();
    }
}

